package com.mf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mf.entity.Customer;
import com.mf.entity.CustomerWarehouse;
import com.mf.entity.Warehouse;
import com.mf.entity.WarehouseDto;
import com.mf.mapper.CustomerMapper;
import com.mf.mapper.WarehouseMapper;
import com.mf.service.ICustomerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author mf
 * @since 2024-04-26
 */
@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements ICustomerService {
    @Resource
    private  WarehouseMapper warehouseMapper;

    @Override
    public List<CustomerWarehouse> getCustomerByWarehouseList(String area) {

        List<WarehouseDto> warehouseList =  warehouseMapper.selectCustomerByWarehouseList(area);

        // 按照客户将仓库信息分组
        Map<Integer, List<WarehouseDto>> collect =
                warehouseList.stream().collect(Collectors.groupingBy(Warehouse::getCustomerId));
        List<CustomerWarehouse> list = collect.entrySet().stream().map(entry -> {
            int customerId = entry.getKey();
            String customerName = entry.getValue().get(0).getCustomerName();
            List<WarehouseDto> warehouseDtoList = entry.getValue();
            return new CustomerWarehouse(customerId, customerName, warehouseDtoList);
        }).toList();
        System.out.println(list);
        return list;
    }
}
